package pl.wroc.pwr.docs.repos.webapp.web;

import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.LinkedList;

import pl.wroc.pwr.docs.repos.webapp.web.Message.TYPE;

public class MessageQueue implements Enumeration<Message> {
	private LinkedList<Message> messages;

	public MessageQueue() {
		messages = new LinkedList<Message>();
	}
	
	public Message pop(){
		return messages.pop();
	}
	
	public void push(Message message){
		messages.add(message);
	}
	
	public boolean empty(){
		return messages.size() == 0;
	}
		
	public void clear(){
		messages.clear();
	}
	
	@Override
	public boolean hasMoreElements() {
		return !empty();
	}

	@Override
	public Message nextElement() {
		return pop();
	}
	
	public static MessageQueue sortByType(MessageQueue queue){
		if(queue != null){
			Collections.sort(queue.messages, new Comparator<Message>() {

				@Override
				public int compare(Message m1, Message m2) {
					TYPE t1 = m1.getType();
					TYPE t2 = m2.getType();
					if(t1 == t2){
						return 0;
					}
					else if(t1 == TYPE.FLASH){
						return 1;
					}
					else{
						return -1;
					}
				}
			});
		}

		return queue;
	}
}